package org.liuhy.line.rect
{
	import org.liuhy.line.AbstractLine;
	import org.liuhy.line.CLine;
	import org.liuhy.node.FlowNode;
	import org.liuhy.node.focus.FocusNode;
	import org.liuhy.utils.LineUtils;
	
	public class LeftFocusUtils
	{
		public function LeftFocusUtils()
		{
		}
		private function _focusNodeArrowLine(p_focusNode:FocusNode,p_line:AbstractLine,p_flowNode:FlowNode,p_function:Function):void{
			var _cLine:CLine = p_line.parent as CLine;
			//竖线;
			if(p_line.x1	==	p_line.x2){
				if((p_focusNode.x+p_flowNode._x-_cLine.x - p_line.x2)>0){
					var _line1:RectLine = new RectLine(p_line.x1,p_focusNode.y+p_flowNode._y-_cLine.y,p_focusNode.x+p_flowNode._x-_cLine.x,p_focusNode.y+p_flowNode._y-_cLine.y,_cLine);
					var _line2:RectLine = new RectLine(p_line.x1,p_line.y1,_line1.x1,_line1.y1,_cLine);
					p_function.apply(null,[p_focusNode,p_line,[_line2,_line1],[p_line],p_line.preLine,null]);
				}else{
					//箭头线在流程左侧节点的右侧;
					if(null!=p_line.preLine){
						var _line1:RectLine = new RectLine(p_focusNode.x+p_flowNode._x-_cLine.x-RectConstants._ADJUST_LEN,p_focusNode.y + p_flowNode._y-_cLine.y,p_focusNode.x+p_flowNode._x-_cLine.x,p_focusNode.y + p_flowNode._y-_cLine.y,_cLine);								
						var _line2:RectLine = new RectLine(_line1.x1,p_line.y1,_line1.x1,_line1.y1,_cLine);	
						var _line3:RectLine = new RectLine(p_line.preLine.x1,p_line.preLine.y1,_line2.x1,_line2.y1,_cLine);
						p_function.apply(null,[p_focusNode,p_line,[_line3,_line2,_line1],[p_line.preLine,p_line],p_line.preLine.preLine,null]);
					}else{
						var _line1:RectLine = new RectLine(p_focusNode.x+p_flowNode._x-_cLine.x-RectConstants._ADJUST_LEN,p_focusNode.y + p_flowNode._y-_cLine.y,p_focusNode.x+p_flowNode._x-_cLine.x,p_focusNode.y + p_flowNode._y-_cLine.y,_cLine);
						var _y2:int = p_line.y1 + (p_line.y2-p_line.y1)/2;//
						var _line2:RectLine = new RectLine(_line1.x1,_y2,_line1.x1,_line1.y1,_cLine);	
						var _line3:RectLine = new RectLine(p_line.x1,_y2,_line2.x1,_line2.y1,_cLine);
						var _line4:RectLine = new RectLine(p_line.x1,p_line.y1,_line3.x1,_line3.y1,_cLine);
						p_function.apply(null,[p_focusNode,p_line,[_line4,_line3,_line2,_line1],[p_line],p_line.preLine,null]);
					}
				}
			}else if(p_line.y1	==	p_line.y2){
				if(p_focusNode.x + p_flowNode._x-_cLine.x > p_line.x1){
					//一条横线;
					if(null==p_line.preLine){
						var _x2:int = p_line.x1 + (p_focusNode.x + p_flowNode._x-_cLine.x -p_line.x1)/2;//
						var _line1:RectLine = new RectLine(_x2,p_focusNode.y + p_flowNode._y-_cLine.y,p_focusNode.x+p_flowNode._x-_cLine.x,p_focusNode.y + p_flowNode._y-_cLine.y,_cLine);
						var _line2:RectLine = new RectLine(_x2,p_line.y2,_line1.x1,_line1.y1,_cLine);	
						var _line3:RectLine = new RectLine(p_line.x1,p_line.y1,_line2.x1,_line2.y1,_cLine);
						p_function.apply(null,[p_focusNode,p_line,[_line3,_line2,_line1],[p_line],p_line.preLine,null]);
					}else{
						var _line1:RectLine = new RectLine(p_line.x1,p_focusNode.y + p_flowNode._y-_cLine.y,p_focusNode.x+p_flowNode._x-_cLine.x,p_focusNode.y + p_flowNode._y-_cLine.y,_cLine);
						var _line2:RectLine = new RectLine(p_line.preLine.x1,p_line.preLine.y1,_line1.x1,_line1.y1,_cLine);	
						p_function.apply(null,[p_focusNode,p_line,[_line2,_line1],[p_line.preLine,p_line],p_line.preLine.preLine,null]);
					}
				}else{
					var _line1:RectLine = new RectLine(p_focusNode.x+p_flowNode._x-_cLine.x-RectConstants._ADJUST_LEN,p_focusNode.y + p_flowNode._y-_cLine.y,p_focusNode.x+p_flowNode._x-_cLine.x,p_focusNode.y + p_flowNode._y-_cLine.y,_cLine);
					var _y2:int = p_line.y1 + (p_line.y2-p_line.y1)/2;//
					var _line2:RectLine = new RectLine(_line1.x1,p_line.y1,_line1.x1,_line1.y1,_cLine);	
					var _line3:RectLine = new RectLine(p_line.x1,p_line.y1,_line2.x1,_line2.y1,_cLine);
					p_function.apply(null,[p_focusNode,p_line,[_line3,_line2,_line1],[p_line],p_line.preLine,null]);
				}				
			}	
		}
		public function arrowNodeFocusAdjustVirtualLine(p_focusNode:FocusNode,p_line:AbstractLine,p_flowNode:FlowNode):void{
			_focusNodeArrowLine(p_focusNode,p_line,p_flowNode,RectLineUtils.drawVirtualLine);
		}

		public function focusNodeDropArrowLine(p_focusNode:FocusNode,p_line:AbstractLine,p_flowNode:FlowNode):void{
			_focusNodeArrowLine(p_focusNode,p_line,p_flowNode,RectLineUtils.drawLine);
		}

		public function focusNodeAdjustArrowLine(p_focusNode:FocusNode,p_line:AbstractLine,p_flowNode:FlowNode):void{
			var _cLine:CLine = p_line.parent as CLine;
			//左侧连接
			if(p_line.x1 > (p_flowNode._x + p_focusNode.x-_cLine.x)){
				//1.创建左侧线段
				if(null!=p_line.preLine){
					var _line1:RectLine = new RectLine(p_flowNode._x+p_focusNode.x-_cLine.x-RectConstants._ADJUST_LEN,p_flowNode._y+p_focusNode.y-_cLine.y,p_flowNode._x+p_focusNode.x-_cLine.x,p_flowNode._y+p_focusNode.y-_cLine.y,p_line.cLine);
					//上一根线；
					//新线坐标(p_line.preLine.x1)
					if(null!=p_line.preLine.preLine){
						//只移动p_line.preLine;
						var _line2:RectLine = new RectLine(_line1.x1,p_line.preLine.y1,_line1.x1,_line1.y1,p_line.cLine);
						p_line.preLine.preLine.x2 = _line2.x1;
						_line1.preLine = _line2;
						_line2.nextLine = _line1;
						p_line.preLine.preLine.nextLine = _line2;
						_line2.preLine = p_line.preLine.preLine;
						p_line.preLine.preLine.draw();
						_line1.draw();
						_line2.draw();									
						LineUtils.removeLine(p_line.preLine);
						LineUtils.removeLine(p_line);
						LineUtils.removeInLine(p_line);
						p_flowNode.addInLine(_line1);
						_line1.in_flowFocusNode = p_focusNode;
					}else{
						//两根线；
						var _y2:int = p_line.preLine.y1 + (p_flowNode._y+p_focusNode.y-_cLine.y-p_line.preLine.y1)/2;//
						var _line_2:RectLine = new RectLine(_line1.x1,_y2,_line1.x1,_line1.y1,p_line.cLine);								
						var _line3:RectLine = new RectLine(p_line.preLine.x1,_line_2.y1,_line_2.x1,_line_2.y1,p_line.cLine);
						var _line4:RectLine = new RectLine(p_line.preLine.x1,p_line.preLine.y1,_line3.x1,_line3.y1,p_line.cLine);
						RectLineUtils.drawLine(p_focusNode,p_line,[_line4,_line3,_line_2,_line1],[p_line.preLine,p_line],p_line.preLine.preLine,null);
					}
				}else{
					//一根直线;
					p_line.x1 = p_flowNode._x + p_focusNode.x-_cLine.x-RectConstants._ADJUST_LEN;
					p_line.y1 = p_flowNode._y + p_focusNode.y-_cLine.y;
					p_line.x2 = p_flowNode._x + p_focusNode.x-_cLine.x;
					p_line.y2 = p_flowNode._y + p_focusNode.y-_cLine.y;					
					p_line.draw();
				}
			}else{
				//两个节点间夹一根线;
				if(null==p_line.preLine && null!=p_line.out_flowFocusNode){
					//折线;
					 var _x2:int = p_line.x1 + (p_focusNode.x+p_flowNode._x-_cLine.x-p_line.x1)/2;
					 var _line1:RectLine = new RectLine(p_line.x1,p_line.y1,_x2,p_line.y1,p_line.cLine);
					 var _line2:RectLine = new RectLine(_line1.x2,_line1.y2,_line1.x2,p_focusNode.y+p_flowNode._y-_cLine.y,p_line.cLine);
					 var _line3:RectLine = new RectLine(_line2.x2,_line2.y2,p_focusNode.x+p_flowNode._x-_cLine.x,p_focusNode.y+p_flowNode._y-_cLine.y,p_line.cLine);
					 RectLineUtils.drawLine(p_focusNode,p_line,[_line1,_line2,_line3],[p_line],p_line.preLine,null);
		 
				}else{
					p_line.x2 = p_flowNode._x+p_focusNode.x-_cLine.x;
					p_line.y2 = p_flowNode._y+p_focusNode.y-_cLine.y;
					p_line.y1 = p_flowNode._y+p_focusNode.y-_cLine.y;
					p_line.draw();
					if(null!=p_line.preLine){
						p_line.preLine.x2 = p_line.x1;
						p_line.preLine.y2 = p_line.y1;
						p_line.preLine.x1 = p_line.x1;								
						p_line.preLine.draw();
						if(null!=p_line.preLine.preLine){
							p_line.preLine.preLine.x2 = p_line.preLine.x1;
							p_line.preLine.preLine.y2 = p_line.preLine.y1;
							p_line.preLine.preLine.draw();
						}								
					}
					if(null!=p_line.preLine && null!=p_line.nextLine){
						p_line.nextLine.x1 = p_line.x2;
						p_line.nextLine.y1 = p_line.y2;
						p_line.nextLine.draw();
					}
					if(null==p_line.preLine && null!=p_line.nextLine){
						p_line.nextLine.x1 = p_line.x2;
						p_line.nextLine.y1 = p_line.y2;
						p_line.nextLine.draw();
					}
					RectLineUtils.toOneLine(p_line);	
				}
			}
		}
		private function _focusNodeFromLine(p_focusNode:FocusNode,p_line:AbstractLine,p_flowNode:FlowNode,p_function:Function):void{
			var _cLine:CLine = p_line.parent as CLine;
			//竖线
			if(p_line.x1	==	p_line.x2){
				//       |
				//       |
				///   ---+---
				///   |      |
				//    +      +
				//    |      |
				//	  ---+---
				//箭头线在流程左侧节点的右侧;
				if(null==p_line.nextLine){
					var _x2:int = 0 ;//
					if(p_focusNode.x + p_flowNode._x-_cLine.x > p_line.x1){
						 _x2 = p_line.x2;
					}else{
						 _x2 = p_focusNode.x+p_flowNode._x-_cLine.x - RectConstants._ADJUST_LEN;
					}
					var _y2:int = p_line.y2 + (p_flowNode._y + p_focusNode.y-_cLine.y -p_line.y2)/2;
					var _line1:RectLine = new RectLine(p_focusNode.x+p_flowNode._x-_cLine.x,p_focusNode.y + p_flowNode._y-_cLine.y,_x2,p_focusNode.y + p_flowNode._y-_cLine.y,_cLine);								
					var _line2:RectLine = new RectLine(_line1.x2,_line1.y2,_line1.x2,_y2,_cLine);	
					var _line3:RectLine = new RectLine(_line2.x2,_line2.y2,p_line.x2,_y2,_cLine);
					var _line4:RectLine = new RectLine(_line3.x2,_line3.y2,p_line.x2,p_line.y2,_cLine);	
					p_function.apply(null,[p_focusNode,p_line,[_line1,_line2,_line3,_line4],[p_line],null,p_line.nextLine]);
				}else{
					//两根
					var _line1:RectLine = new RectLine(p_focusNode.x+p_flowNode._x-_cLine.x,p_focusNode.y + p_flowNode._y-_cLine.y,p_focusNode.x+p_flowNode._x-_cLine.x-RectConstants._ADJUST_LEN,p_focusNode.y + p_flowNode._y-_cLine.y,_cLine);
					var _y2:int = p_line.y2 + (p_focusNode.y + p_flowNode._y-_cLine.y-p_line.y2)/2;//
					var _line2:RectLine = new RectLine(_line1.x2,_line1.y2,_line1.x2,p_line.y2,_cLine);	
					var _line3:RectLine = new RectLine(_line2.x2,_line2.y2,p_line.nextLine.x2,p_line.nextLine.y2,_cLine);
					p_function.apply(null,[p_focusNode,p_line,[_line1,_line2,_line3],[p_line,p_line.nextLine],null,p_line.nextLine.nextLine]);
				}
//				else{
//					//三根
//					var _line1:RectLine = new RectLine(p_focusNode.x+p_flowNode._x-_cLine.x,p_focusNode.y + p_flowNode._y-_cLine.y,p_focusNode.x+p_flowNode._x-_cLine.x-RectConstants._ADJUST_LEN,p_focusNode.y + p_flowNode._y-_cLine.y,_cLine);
//					var _y2:int = p_line.y2 + (p_focusNode.y + p_flowNode._y-_cLine.y-p_line.y2)/2;//
//					var _line2:RectLine = new RectLine(_line1.x2,_line1.y2,_line1.x2,p_line.y2,_cLine);	
//					var _line3:RectLine = new RectLine(_line2.x2,_line2.y2,p_line.nextLine.x2,p_line.nextLine.y2,_cLine);
//					p_function.apply(null,[p_focusNode,p_line,[_line1,_line2,_line3],[p_line,p_line.nextLine],null,p_line.nextLine.nextLine]);
//				}
			}else if(p_line.y1	==	p_line.y2){
				if(null==p_line.nextLine){
					//一条横线;
					var _x2:int = 0 ;//
					if(p_focusNode.x + p_flowNode._x-_cLine.x > p_line.x1){
						 _x2 = p_line.x1 + (p_focusNode.x + p_flowNode._x-_cLine.x -p_line.x1)/2;//
					}else{
						 _x2 = p_focusNode.x+p_flowNode._x-_cLine.x -RectConstants._ADJUST_LEN;
					}
					var _line1:RectLine = new RectLine(p_focusNode.x+p_flowNode._x-_cLine.x,p_focusNode.y + p_flowNode._y-_cLine.y,_x2,p_focusNode.y + p_flowNode._y-_cLine.y,_cLine);
					var _line2:RectLine = new RectLine(_line1.x2,_line1.y2,_x2,p_line.y1,_cLine);	
					var _line3:RectLine = new RectLine(_line2.x2,_line2.y2,p_line.x2,p_line.y2,_cLine);
					p_function.apply(null,[p_focusNode,p_line,[_line1,_line2,_line3],[p_line],null,p_line.nextLine]);
				}else{
					//两条及以上横线;
					if(p_focusNode.x + p_flowNode._x-_cLine.x > p_line.x2){
						//焦点在x2的右侧;2根线
						var _x2:int = 0;//
						if(p_focusNode.x + p_flowNode._x-_cLine.x > p_line.x1){
							 _x2 = p_line.x1 + (p_focusNode.x + p_flowNode._x-_cLine.x -p_line.x1)/2;//
						}else{
							 _x2 = p_focusNode.x+p_flowNode._x-_cLine.x -RectConstants._ADJUST_LEN;
						}
						var _line1:RectLine = new RectLine(p_focusNode.x+p_flowNode._x-_cLine.x,p_focusNode.y + p_flowNode._y-_cLine.y,p_line.x2,p_focusNode.y + p_flowNode._y-_cLine.y,_cLine);
						var _line2:RectLine = new RectLine(_line1.x2,_line1.y2,p_line.nextLine.x2,p_line.nextLine.y2,_cLine);
						p_function.apply(null,[p_focusNode,p_line,[_line1,_line2],[p_line,p_line.nextLine],null,p_line.nextLine.nextLine]);
					}else{
						var _x2:int = 0;//
						if(p_focusNode.x + p_flowNode._x-_cLine.x > p_line.x1){
							 _x2 = p_line.x1 + (p_focusNode.x + p_flowNode._x-_cLine.x -p_line.x1)/2;//
						}else{
							 _x2 = p_focusNode.x+p_flowNode._x-_cLine.x -RectConstants._ADJUST_LEN;
						}
						var _line1:RectLine = new RectLine(p_focusNode.x+p_flowNode._x-_cLine.x,p_focusNode.y + p_flowNode._y-_cLine.y,_x2,p_focusNode.y + p_flowNode._y-_cLine.y,_cLine);
						var _line2:RectLine = new RectLine(_line1.x2,_line1.y2,_line1.x2,p_line.y1,_cLine);
						var _line3:RectLine = new RectLine(_line2.x2,_line2.y2,p_line.x2,p_line.y2,_cLine);
						p_function.apply(null,[p_focusNode,p_line,[_line1,_line2,_line3],[p_line],null,p_line.nextLine]);
					}
				}
			}	
		}
		public function fromNodeFocusAdjustVirtualLine(p_focusNode:FocusNode,p_line:AbstractLine,p_flowNode:FlowNode):void{
			_focusNodeFromLine(p_focusNode,p_line,p_flowNode,RectLineUtils.drawVirtualLine);
		}
		public function focusNodeDropFromLine(p_focusNode:FocusNode,p_line:AbstractLine,p_flowNode:FlowNode):void{
			_focusNodeFromLine(p_focusNode,p_line,p_flowNode,RectLineUtils.drawFromLine);
		}
		public function focusNodeAdjustFromLine(p_focusNode:FocusNode,p_line:AbstractLine,p_flowNode:FlowNode):void{
			var _cLine:CLine = p_line.parent as CLine;
			//左侧连接
			if(p_line.x2 > (p_flowNode._x + p_line.out_flowFocusNode.x-_cLine.x)){
				//1.创建左侧线段
				if(null!=p_line.nextLine){
					var _line1:RectLine = new RectLine(p_line.fromLineNode.x-_cLine.x,p_line.fromLineNode.y-_cLine.y,p_line.fromLineNode.x-_cLine.x-RectConstants._ADJUST_LEN,p_line.fromLineNode.y-_cLine.y,p_line.cLine);
					//上一根线；
					//新线坐标(p_line.preLine.x1)
					if(null!=p_line.nextLine.nextLine){
						//只移动p_line.preLine;
						var _line2:RectLine = new RectLine(_line1.x2,_line1.y2,_line1.x2,p_line.nextLine.y2,p_line.cLine);
						p_line.nextLine.nextLine.x1 = _line2.x2;
						_line1.nextLine = _line2;
						_line2.preLine = _line1;
						p_line.nextLine.nextLine.preLine = _line2;
						_line2.nextLine = p_line.nextLine.nextLine;
						p_line.nextLine.nextLine.draw();
						_line1.draw();
						_line2.draw();									
						LineUtils.removeLine(p_line.nextLine);
						LineUtils.removeLine(p_line);
						LineUtils.removeOutLine(p_line);
						p_flowNode.addOutLine(_line1);
						_line1.out_flowFocusNode = p_line.out_flowFocusNode;
					}else{
						//两根线;
						var _y2:int 		= p_line.nextLine.y2 + (p_line.fromLineNode.y-_cLine.y-p_line.nextLine.y2)/2;//
						var _line_2:RectLine 	= new RectLine(_line1.x2,_line1.y2,_line1.x2,_y2,p_line.cLine);								
						var _line3:RectLine 	= new RectLine(_line_2.x2,_line_2.y2,p_line.nextLine.x1,_line_2.y2,p_line.cLine);
						var _line4:RectLine 	= new RectLine(_line3.x2,_line3.y2,p_line.nextLine.x2,p_line.nextLine.y2,p_line.cLine);
						_line1.nextLine 	= _line_2;
						_line_2.preLine 	= _line1;
						_line_2.nextLine 	= _line3;
						_line3.preLine 		= _line_2;
						_line3.nextLine 	= _line4;
						_line4.preLine 		= _line3;
						//连接到另一个流程节点;
						if(null!=p_line.nextLine.in_flowFocusNode){
							_line4.in_flowFocusNode = p_line.nextLine.in_flowFocusNode;
							var _flowNode1:FlowNode = p_line.nextLine.in_flowFocusNode.parent as FlowNode;
							LineUtils.removeInLine(p_line.nextLine);
							_flowNode1.addInLine(_line4);							
						}
						LineUtils.removeLine(p_line.nextLine);	
						LineUtils.removeLine(p_line);	
						_line1.draw();
						_line_2.draw();
						_line3.draw();
						_line4.draw();	
						LineUtils.removeOutLine(p_line);
						p_flowNode.addOutLine(_line1);
						_line1.out_flowFocusNode = p_line.out_flowFocusNode;

					}
				}else{
					//一根直线;
					p_line.x1 = p_line.fromLineNode.x-_cLine.x;
					p_line.y1 = p_line.fromLineNode.y-_cLine.y;
					p_line.x2 = p_line.fromLineNode.x-_cLine.x-RectConstants._ADJUST_LEN;
					p_line.y2 = p_line.fromLineNode.y-_cLine.y;
					p_line.draw();
				}
			}else{
				//两个节点间夹一根线;
				if(null==p_line.nextLine && null!=p_line.in_flowFocusNode){
					//折线;
					 var _x2:int = p_line.x2 + (p_focusNode.x+p_flowNode._x-_cLine.x-p_line.x2)/2;
					
					 var _line1:RectLine = new RectLine(p_focusNode.x+p_flowNode._x-_cLine.x,p_focusNode.y+p_flowNode._y-_cLine.y,_x2,p_focusNode.y+p_flowNode._y-_cLine.y,p_line.cLine);
					 var _line2:RectLine = new RectLine(_line1.x2,_line1.y2,_line1.x2,p_line.y1,p_line.cLine);
					 var _line3:RectLine = new RectLine(_line2.x2,_line2.y2,p_line.x2,p_line.y2,p_line.cLine);
					 _line1.nextLine = _line2;
					 _line2.preLine  = _line1;
					 _line2.nextLine = _line3;
					 _line3.preLine  = _line2;
					 _line1.out_flowFocusNode = p_focusNode;
					 LineUtils.removeOutLine(p_line);					 
					 p_flowNode.addOutLine(_line1);
					 _line1.draw();
					 _line2.draw();
					 _line3.draw();
					 _line3.in_flowFocusNode = p_line.in_flowFocusNode;
					 (p_line.in_flowFocusNode.parent as FlowNode).removeInLine(p_line);
					 (p_line.in_flowFocusNode.parent as FlowNode).addInLine(_line3);
					 LineUtils.removeLine(p_line);					 
				}else{
					p_line.x1 = p_line.fromLineNode.x-_cLine.x;
					p_line.y1 = p_line.fromLineNode.y-_cLine.y;
					p_line.y2 = p_line.fromLineNode.y-_cLine.y;
					p_line.draw();
					if(null!=p_line.nextLine){
						p_line.nextLine.x1 = p_line.x2;
						p_line.nextLine.y1 = p_line.y2;
						p_line.nextLine.x1 = p_line.x1;								
						p_line.nextLine.draw();
						if(null!=p_line.nextLine.nextLine){
							p_line.nextLine.nextLine.x1 = p_line.nextLine.x2;
							p_line.nextLine.nextLine.y1 = p_line.nextLine.y2;
							p_line.nextLine.nextLine.draw();
						}								
					}
					if(null!=p_line.preLine && null!=p_line.nextLine){
						p_line.nextLine.x1 = p_line.x2;
						p_line.nextLine.y1 = p_line.y2;
						p_line.nextLine.draw();
					}
					if(null==p_line.preLine && null!=p_line.nextLine){
						p_line.nextLine.x1 = p_line.x2;
						p_line.nextLine.y1 = p_line.y2;
						p_line.nextLine.draw();
					}	
					RectLineUtils.toOneLine(p_line);	
				}
			}
		}
	}
}